import { Controller, Get, Param, Req } from '@nestjs/common';
import { ApiHeader, ApiTags } from '@nestjs/swagger';
import { Request } from 'express';
import { RankingListItem } from './interface/ranking-list.interface';
import { RanklingListService } from './rankling-list.service';

@Controller('rankling-list')
@ApiTags('ranking-list')
@ApiHeader({
  name: 'token',
  description: '身份令牌',
})
export class RanklingListController {
  constructor(private readonly ranklingListService: RanklingListService) {}
  @Get('/:Limit')
  async rankingListByTomato(
    @Param('Limit') limit: string,
    @Req() request: Request,
  ) {
    const openid = request['user'].openid;
    const data = await this.ranklingListService.getListByTomato(Number(limit));
    const selfIndex = data.findIndex((item) => {
      if (item.openid === openid) {
        return true;
      }
    });
    const rankingList: Array<RankingListItem> = [];
    data.forEach((item, index) => {
      const { nickname, head_image, property } = item;
      rankingList.push({
        nickname,
        head_image,
        tomato: property.tomato,
        index: index + 1,
      });
    });
    return {
      success: true,
      rankingList,
      selfIndex: selfIndex === -1 ? -1 : selfIndex + 1,
    };
  }
}
